<?php
declare(strict_types=1);
namespace App\Api;
use App\ApiController;

// hook api_mouse_use.php

/**
 * @title   鼠标轨迹
 * @rank    3
 */
class Mouse extends ApiController
{
    // hook api_mouse_start.php


    /**
     * @title   滑块轨迹
     * @rank    99
     */

    public function action_Slider(){

        $zoom = intval(bcadd($this->request->param('z',"260"),"0",0));
        $sx = intval(bcadd($this->request->param('w',"1"),"0",0));
        $sy = intval(bcadd($this->request->param('h',"1"),"0",0));

        $zoom==0 && $zoom= 260;
        mt_srand();
        $middle_x = mt_rand($sx,$sx+20);
        $middle_y = mt_rand($sy,$sy+20);
        $end_x = mt_rand($sx+$zoom,$sx+$zoom+30);
        $end_y = mt_rand($sy,$sy+30);

        $mousedata = $this->fourthOrderBezierMake($middle_x,$middle_y,$end_x,$end_y,intval($zoom/2));
        //return $this->View(get_defined_vars());
        $this->response('0000',['data'=>$mousedata]);
    }

    /**
     * @title   滑块轨迹
     * @rank    99
     */

    public function action_Sliderv(){

        $zoom = intval(bcadd($this->request->param('z',"260"),"0",0));
        $sx = intval(bcadd($this->request->param('w',"240"),"0",0));
        $sy = intval(bcadd($this->request->param('h',"323"),"0",0));
        $zoom==0 && $zoom= 260;
        mt_srand();
        $middle_x = mt_rand($sx,$sx+20);
        $middle_y = mt_rand($sy,$sy+20);
        $end_x = mt_rand($sx+$zoom,$sx+$zoom+30);
        $end_y = mt_rand($sy,$sy+30);
        $mousedata=[];
        for ($i=0;$i<100;$i++){
            $mousedata[] = $this->fourthOrderBezierMake($middle_x,$middle_y,$end_x,$end_y,intval($zoom/2));
        }
        return $this->View(get_defined_vars());
    }




    public function fourthOrderBezierMake($startX, $startY, $endX, $endY, $numSteps=260) {
        $path = [];
        mt_srand();
        $min2 = min($startY,$endY);
        $max2 = max($startY,$endY);
        $lenght = $endX-$startX;
        $add = mt_rand(intval($lenght*0.1),intval($lenght*0.3));
        $p1_x = $startX + $add;
        $p1_y = mt_rand($min2-5,$max2+5);
        $add = mt_rand(  intval(($lenght-$add)*0.1),intval(($lenght-$add)*0.2));
        $p2_x = $p1_x + $add;
        $p2_y = mt_rand($min2-5,$max2+5);
        $p0 = [$startX, $startY];  // 第一个控制点
        $p1 = [$p1_x, $p1_y]; // 第二个控制点
        $p2 = [$p2_x, $p2_y];  // 第三个控制点
        $p3 = [$endX, $endY];  // 第四个控制点

        $z = 1;
        $l = 0;
        for ($i = 0; $i <= $numSteps; $i++) {
            $t = $i / $numSteps;
            $point = $this->fourthOrderBezier($t,$p0, $p1, $p2, $p3);
            if($l>0){
              $z = intval($point[0])-$l;
              if($z>4){
                  $z=2;
              }elseif($z==3){
                  $z=3;
              }elseif($z==1){
                  $z=4;
              }else{
                  $z=2;
              }
            }
            $l=intval($point[0]);
            $path[] = ['x' => $l, 'y' =>intval($point[1]),'z'=>$z];
        }

        return $path;
    }

    public  function fourthOrderBezier($t, $p0, $p1, $p2, $p3) {
        $x = pow(1 - $t, 4) * $p0[0] + 4 * pow(1 - $t, 3) * $t * $p1[0] + 6 * pow(1 - $t, 2) * pow($t, 2) * $p2[0] + 4 * (1 - $t) * pow($t, 3) * $p3[0] + pow($t, 4) * $p3[0];
        $y = pow(1 - $t, 4) * $p0[1] + 4 * pow(1 - $t, 3) * $t * $p1[1] + 6 * pow(1 - $t, 2) * pow($t, 2) * $p2[1] + 4 * (1 - $t) * pow($t, 3) * $p3[1] + pow($t, 4) * $p3[1];
        return [$x, $y];
    }

    public function calculateMousePath($startX, $startY, $endX, $endY, $numSteps=100) {
        $path = [];

        for ($i = 0; $i <= $numSteps; $i++) {
            $t = $i / $numSteps;
            $x = (1 - $t) * $startX + $t * $endX;
            $y = (1 - $t) * $startY + $t * $endY;
            $path[] = ['x' => intval($x), 'y' =>intval($y)];
        }

        return $path;
    }

    public function fast($x1,$y1,$x2,$y2,$xx=100){
        $data = [];
        $min1 = min($x1,$x2);
        $max1 = max($x1,$x2);
        $min2 = min($y1,$y2);
        $max2 = max($y1,$y2);
        mt_srand();
        $p1_x = mt_rand($min1,$max1);
        $p1_y = mt_rand($min2,$max2);
        $p2_x = mt_rand($min1,$max1);
        $p2_y = mt_rand($min2,$max2);
        $t = 0.01;
        $i=0;
        while ($t<1){
            $t += 0.01;
            $temp = 1 - $t;
            $data[$i]["x"]=intval( $x1 *  pow($temp, 3) + 3 * $p1_x * $t * pow($temp, 2) + 3 * $p2_x * pow($t,2) * $temp + $x2 * pow($t,3));
            $i++;
        }
        $t = 0.01;

        $i=0;
        while ($t<1){
            $t += 0.01;
            $temp = 1 - $t;
            $data[$i]["y"]=intval( $y1 *  pow($temp, 3) + 3 * $p1_y * $t * pow($temp, 2) + 3 * $p2_y * pow($t,2) * $temp + $y2 * pow($t,3));

            $i++;
        }

        return $data;
    }

    // hook index_index_end.php
}

?>